<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Frame;
use Part;

class DeliverSchedule extends Model
{
    //
    protected $table = 'deliver_schedule';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function deliver()
    {
        return $this->belongsTo('App\Models\Home\Deliver','deliver_id','id');
    }

    public function getResultSchedule($result=array(),$user=null){
        $list = [];

        foreach ($result as $k => $v){
            $list[$k] = [];
        }



        if (is_null($user)){
            return $list;
        }

        $user->load('frame');
        $frame_id = $user->frame?$user->frame->id:0;
        $member = Frame::getMember($frame_id);
        $member[] = Auth::id();

        $member = array_merge($member,Part::getPartUser($member));

        $query = self::orderBy('id','desc');

        $Schedule_id = [];
        if (!empty($member)){
            $resume = Resume::whereIn('assign',$member)->with(['deliver'=>function($query){
                $query->with(['check_schedule'=>function($query){
                    $query->orderBy('id','desc');
                }]);

            }])->get();

            if($resume){
                foreach ($resume as $key => $val){
                    if ($val->deliver && count($val->deliver)){
                        foreach ($val->deliver as $k => $v){
                            if ($v->check_schedule){
                                $Schedule_id[] = $v->check_schedule->id;
                            }else{
                                $val->deliver->load(['position.client','resume.assign_user']);
                                if(count($val->deliver)){
                                    foreach($val->deliver as $item){
                                        $obj = new DeliverSchedule();
                                        $obj->push_time = $item->created_at->toDateTimeString();
                                        $obj->client_name = $item->position?$item->position->client?$item->position->client->chinese_name:'':'';
                                        $obj->position_name = $item->position?$item->position->title:'';
                                        $obj->resume_name = $item->resume?$item->resume->name:'';
                                        $obj->result_name = '待推荐';
                                        $obj->assign_name = $item->resume?$item->resume->assign_user?$item->resume->assign_user->username:'':'';
                                        $list[1][] = $obj;
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }

        if (!empty($Schedule_id)){
            $query = $query->whereIn('id',$Schedule_id);
        }else{
            return $list;
        }

        $query = $query->with(['deliver'=>function($query){
            $query->with(['position.client','resume.assign_user']);
        }]);

        $res = $query->get()->groupBy('deliver_id');

        $data = [];

        foreach ($res as $key => $val){

            $data[] = $val[0];

        }

        foreach($data as $item){

            foreach ($result as $k => $v){

                if ($item->result == $k){

                    $item->result_name = $v;

                    $list[$k][] = $item;

                }

            }

        }

        foreach ($list as $k => $v){

            foreach ($v as $i => $item){

                if ($item && isset($item->deliver)){

                    $list[$k][$i]->push_time = $item->deliver?$item->deliver->created_at->toDateTimeString():'';

                    $list[$k][$i]->position_name = $item->deliver?$item->deliver->position?$item->deliver->position->title:'':'';

                    $list[$k][$i]->resume_name = $item->deliver?$item->deliver->resume?$item->deliver->resume->name:'':'';

                    $list[$k][$i]->client_name = $item->deliver?$item->deliver->position?$item->deliver->position->client?$item->deliver->position->client->chinese_name:'':'':'';

                    $list[$k][$i]->assign_name = $item->deliver?$item->deliver->resume?$item->deliver->resume->assign_user?$item->deliver->resume->assign_user->username:'':'':'';

                }

            }

        }

        return $list;

    }
}
